package com.example.ymm.modules1.service.impl.ms;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.Slave;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.ms.DDepartment;
import com.example.ymm.modules1.entity.my.sys.SysMenu;
import com.example.ymm.modules1.mapper.ms.DDepartmentMapper;
import com.example.ymm.modules1.query.ms.DDepartmentQueryCriteria;
import com.example.ymm.modules1.service.ms.DDepartmentService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
@Slf4j
@DS("slave")
public class DDepartmentServiceImpl extends BaseServiceImpl<DDepartmentMapper, DDepartment> implements DDepartmentService {

    @Override
    public TableList<DDepartment> list(DDepartmentQueryCriteria criteria, YmPageDto pageDto) {
        IPage<DDepartment> page = new Page<DDepartment>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DDepartment.class, criteria);
//        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<DDepartment> tableList=new TableList(page);
        return tableList;
    }

    public List<DDepartment> queryAll(DDepartmentQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(DDepartment.class, criteria);
//        predicate.orderByDesc("create_time");
        List<DDepartment> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public List<DDepartment> getTreeList() {
        DDepartmentQueryCriteria criteria=new DDepartmentQueryCriteria();
        List<DDepartment> arr = queryAll(criteria);
        List<DDepartment> lists=new ArrayList<>();
        arr.forEach(e->{
            if("0".equals(e.getParent())){
                forEachDeptList(e,arr);
                lists.add(e);
            }
        });
        return lists;
    }

    public void forEachDeptList(DDepartment parent, List<DDepartment> list){

        List<DDepartment> children=new ArrayList<>();
        list.forEach(e->{
            if(parent.getOid().equals(e.getParent())){
                children.add(e);
            }
        });

        if(UtilValidate.isNotEmpty(children)){
//            children.sort(new Comparator<DDepartment>() {
//                @Override
//                public int compare(DDepartment o1, DDepartment o2) {
//                    return o1.getSort()-o2.getSort();
//                }
//            });
            parent.setChildren(children);

            children.forEach(r->{
                forEachDeptList(r,list);
            });
        }

    }


    @Override
    public DDepartment getByDeptNo(String no) {
        DynamicDataSourceContextHolder.push("slave");
        DDepartmentQueryCriteria criteria=new DDepartmentQueryCriteria();
        criteria.setTid(no);
        List<DDepartment> dDepartments = queryAll(criteria);
        DynamicDataSourceContextHolder.clear();
        DynamicDataSourceContextHolder.push("slave2");
        DDepartment dDepartment=null;
        if(UtilValidate.isNotEmpty(dDepartments)){
            dDepartment=dDepartments.get(0);
        }
        return dDepartment;
    }
}
